Skip to main content

Create vehicle statistics

The example below shows how to create statistics of all registered vehicles in Norway, including geographical information related to the vehicle owner, which is linked to the vehicle dataset via a personal identifier.

In this case, a vehicle dataset is used as the basis, since the focus is on vehicles (and not people). Vehicles with missing links to the municipality are due to the owner not being resident in Norway at the given time (1/1 2024). These are classified as "Not resident in Norway" in the geographical statistics.

 require no.ssb.fdb:32 as db

// Creating vehicle dataset
create-dataset vehicles
import db/KJORETOY_KJT_GRUP 2023-12-31 as vehicle_group
import db/KJORETOY_KJ_ART 2023-12-31 as type_of_use
import db/KJORETOY_FREG_AR 2023-12-31 as model_year
import db/KJORETOY_BRUKTIMP 2023-12-31 as imported_used

import db/KJORETOY_DRIVSTOFF_OMK 2023-12-31 as fuel_type
import db/KJORETOY_TEKN_CO2_UTSLIPP 2023-12-31 as co2_emission
import db/KJORETOY_TEKN_DRIVSTOFF_FORBRUK 2023-12-31 as fuel_consumption

import db/KJORETOY_EG_VEKT 2023-12-31 as weight
import db/KJORETOY_KJORETOYID_FNR 2023-12-31 as idnr

textblock
Overview of vehicle types and usage
endblock
tabulate vehicle_group
clone-variables vehicle_group -> vehicle_group_gr
destring vehicle_group_gr
recode vehicle_group_gr (101 = 1 'Car')(106/107 = 2 'Car ambulances')(201/215 = 3 'Bus')(301/315 = 4 'Van/combined van')(316 = 5 'Campervan')(320/336 = 6 'Truck')(360/381 = 6)(340 = 7 'Tractor unit')(350 = 8 'Tracked vehicle')(401 = 9 'Tractor')(501/509 = 10 'Motor implement')(601/621 = 11 'Moped/motorcycle')(630 = 12 'Snowmobile')
piechart vehicle_group_gr
tabulate vehicle_group_gr, freq cellpct
tabulate vehicle_group_gr type_of_use, rowpct

textblock
Vehicle age distributed by vehicle type
endblock
generate age = 2024 - model_year
tabulate vehicle_group_gr, summarize(age) rowsort
barchart(mean) age, over(vehicle_group_gr)

textblock
Proportion of imported used vehicles distributed by vehicle type
endblock
tabulate vehicle_group_gr imported_used, rowpct

destring fuel_type
recode fuel_type (3 4 6 9 10 11 12 13 14 15 = 9)(7 8 = 3 'Hybrid')(5 = 4 'Electric')

generate car = vehicle_group_gr == 1
generate fossil_car = inlist(fuel_type,1,2) 
generate hybrid_car = fuel_type == 3
generate electric_car = fuel_type == 4

textblock
Vehicles distributed by fuel type and vehicle type
endblock
tabulate vehicle_group_gr fuel_type, missing rowpct

textblock
Cars' age distributed by fuel type (sorted by age)
endblock
tabulate fuel_type if car, summarize(age) rowsort

textblock
CO2 emissions distributed by vehicle type (sorted by CO2 emissions)
endblock
tabulate vehicle_group_gr, summarize(co2_emission) rowsort
barchart(mean) co2_emission, over(vehicle_group_gr)

textblock
Fuel consumption distributed by vehicle type (sorted by fuel consumption)
endblock
tabulate vehicle_group_gr, summarize(fuel_consumption) rowsort
barchart(mean) fuel_consumption, over(vehicle_group_gr)

textblock
Vehicle weight distributed by vehicle type (sorted by weight)
endblock
tabulate vehicle_group_gr, summarize(weight) rowsort
barchart(mean) weight, over(vehicle_group_gr)

textblock
Vehicle weight distributed by fuel type and vehicle type (sorted by vehicle type average weight)
endblock
tabulate vehicle_group_gr fuel_type, summarize(weight) rowsort
barchart(mean) weight, over(fuel_type)


// Creating person dataset and linking residence information to the vehicle dataset
create-dataset population
import db/BEFOLKNING_KOMMNR_FORMELL 2024-01-01 as municipality
merge municipality into vehicles on idnr


// Creating geographical statistics for vehicles
use vehicles
generate county = substr(municipality,1,2)
replace county = '99' if sysmiss(county)
define-labels county_label '03' Oslo '11' Rogaland '15' 'Møre og Romsdal' '18' Nordland '31' Østfold '32' Akershus '33' Buskerud '34' Innlandet '39' Vestfold '40' Telemark '42' Agder '46' Vestland '50' Trøndelag '55' Troms '56' Finnmark '99' 'Not residing in Norway'
assign-labels county county_label

//Cloning municipality variable and extracting cities from one variable (the rest are placed in a combined category)
clone-variables municipality -> city
replace city = '9999' if sysmiss(city)
replace city = '8888' if !inlist(city,'0301','1103','3201','3301','4204','4601','5001','5501','9999')
recode city ('8888' = '8888' 'Other municipalities')('9999' = '9999' 'Not residing in Norway')

textblock
Number of vehicles distributed by owner's place of residence
endblock
tabulate county
barchart(count) county

tabulate county vehicle_group_gr, rowpct
tabulate city vehicle_group_gr, rowpct

barchart(count) vehicle_group_gr, over(county) stack
barchart(percent) vehicle_group_gr, over(county) stack

barchart(count) fuel_type, over(county) stack
barchart(percent) fuel_type, over(county) stack

barchart(percent) vehicle_group_gr, over(city) stack
barchart(percent) fuel_type, over(city) stack